package com.ruoyi.doc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.doc.domain.Content;
import com.ruoyi.doc.domain.ContentType;
import com.ruoyi.doc.service.IContentService;
import com.ruoyi.framework.config.ServerConfig;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.service.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文档Controller
 * 
 * @author yuanpingfang
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/doc/content")
public class ContentController extends BaseController
{
    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IContentService contentService;

    /**
     * 查询文档列表
     */
    @PreAuthorize("@ss.hasPermi('doc:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(Content content)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = "";
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        }
        Map<String, Object> returnMap = contentService.selectContentList(content, pageNum, pageSize, orderBy);
        TableDataInfo rspData = new TableDataInfo();
        Map<String,Object> param = new HashMap<>();
        param.put("serverUrl",serverConfig.getUrl());
        rspData.setParam(param);
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        if (!ServiceUtil.isSuccess(returnMap)) {
            rspData.setRows(Collections.emptyList());
            rspData.setTotal(0);
            return rspData;
        }
        List<ContentType> list = UtilGenerics.checkList(returnMap.get("rows"));
        Integer total = UtilMisc.toInteger(returnMap.get("total"));
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 导出文档列表
     */
    @PreAuthorize("@ss.hasPermi('doc:content:export')")
    @Log(title = "文档", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Content content)
    {
        Map<String, Object> returnMap = contentService.selectContentList(content, 1, 999999, null);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        List<ContentType> list = UtilGenerics.checkList(returnMap.get("rows"));
        ExcelUtil<ContentType> util = new ExcelUtil<ContentType>(ContentType.class);
        return util.exportExcel(list, "content");
    }

    /**
     * 获取文档详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:content:query')")
    @GetMapping(value = "/{contentId}")
    public AjaxResult getInfo(@PathVariable("contentId") String contentId)
    {
        Map<String, Object> returnMap = contentService.selectContentById(contentId);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success(returnMap);
    }

    /**
     * 上传文件
     */
    @PreAuthorize("@ss.hasPermi('doc:content:add')")
    @Log(title = "文档", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Content content)
    {
        Map<String, Object> returnMap = contentService.insertContent(content);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }

    /**
     * 移动文件或文件夹
     */
    @PreAuthorize("@ss.hasPermi('doc:content:move')")
    @Log(title = "移动文件或文件夹", businessType = BusinessType.INSERT)
    @GetMapping("/move/{targetContentId}/{contentIds}")
    public AjaxResult move(@PathVariable String targetContentId,@PathVariable String[] contentIds)
    {
        Map<String, Object> returnMap = contentService.moveContent(targetContentId,contentIds);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }

    /**
     * 修改文档
     */
    @PreAuthorize("@ss.hasPermi('doc:content:edit')")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Content content)
    {
        Map<String, Object> returnMap = contentService.updateContent(content);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }

    /**
     * 删除文档
     */
    @PreAuthorize("@ss.hasPermi('doc:content:remove')")
    @Log(title = "文档", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contentIds}")
    public AjaxResult remove(@PathVariable String[] contentIds)
    {
        Map<String, Object> returnMap = contentService.deleteContentByIds(contentIds);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }

    /**
     * 文档提交审批
     */
    @PreAuthorize("@ss.hasPermi('doc:content:start')")
    @Log(title = "文档提交流程审批", businessType = BusinessType.START)
    @GetMapping("/startWorkflow/{contentId}")
    public AjaxResult startWorkflow(@PathVariable String contentId)
    {
        Map<String, Object> returnMap = contentService.startWorkflowContentById(contentId);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }



}
